package com.giscloud.amp.service.auth.impl;

import com.giscloud.amp.dao.AmpMapRegistMapper;
import com.giscloud.amp.entity.AmpMapRegist;
import com.giscloud.amp.entity.dto.AmpMapRegistDTO;
import com.giscloud.amp.entity.vo.AmpMapRegistVO;
import com.giscloud.amp.service.auth.AmpMapRegistService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:gxd
 * @Description: 权限服务类
 * @Date: 17:19 2018/2/24
 * @Modified By:
 */
@Service("ampMapRegistService")
@CacheConfig(cacheNames ="amp:ampMapRegistService")
public class AmpMapRegistServiceImpl implements AmpMapRegistService {
    private final static Logger log = LoggerFactory.getLogger(AmpMapRegistServiceImpl.class);

    @Autowired
    private AmpMapRegistMapper ampMapRegistMapper;

    @Override
    public List<Map<String, String>> getDictName() {
        return ampMapRegistMapper.getDictName();
    }

    @Override
    public List<AmpMapRegistVO> findMapByCityId(AmpMapRegistDTO ampMapRegistDTO) {
        AmpMapRegist ampMapRegist = new AmpMapRegist();
        BeanUtils.copyProperties(ampMapRegistDTO,ampMapRegist);
        return ampMapRegistMapper.findMapByCityId(ampMapRegist);
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    @Override
    public int insert(AmpMapRegistDTO ampMapRegistDTO) {
        //判断同名重复的
        AmpMapRegist judge = new AmpMapRegist();
        judge.setCityId(ampMapRegistDTO.getCityId());
        judge.setMapUrl(ampMapRegistDTO.getMapUrl());
        List<AmpMapRegistVO> data = ampMapRegistMapper.findMapByCityId(judge);
        if(data.size()>0){
            return -1;
        }

        AmpMapRegist ampMapRegist = new AmpMapRegist();
        BeanUtils.copyProperties(ampMapRegistDTO,ampMapRegist);
        ampMapRegist.setStatusCd(1);
        ampMapRegist.setCreatedDt(new Date());
        ampMapRegist.setVersion(new Date());
        return ampMapRegistMapper.insert(ampMapRegist);
    }

     @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    @Override
    public int update(AmpMapRegistDTO ampMapRegistDTO) {
        AmpMapRegistVO ampMapRegistVO = null;
        AmpMapRegist judge = new AmpMapRegist();
        judge.setCityId(ampMapRegistDTO.getCityId());
        judge.setMapType(ampMapRegistDTO.getMapType());
        judge.setMapUrl(ampMapRegistDTO.getMapUrl());
        List<AmpMapRegistVO> data = ampMapRegistMapper.findMapByCityId(judge);
        if(data.size()>1){
            return -1;
        }
        if(data.size()==1){
            ampMapRegistVO = data.get(0);
            if(!ampMapRegistDTO.getMapId().toString().equals(ampMapRegistVO.getMapId().toString())){
                return -1;
            }
        }
        AmpMapRegist ampMapRegist = new AmpMapRegist();
        BeanUtils.copyProperties(ampMapRegistDTO,ampMapRegist);
        ampMapRegist.setVersion(new Date());
        return ampMapRegistMapper.update(ampMapRegist);
    }

    @Override
    public int delete(AmpMapRegistDTO ampMapRegistDTO) {
        AmpMapRegist ampMapRegist = new AmpMapRegist();
        ampMapRegist.setMapId(ampMapRegistDTO.getMapId());
        AmpMapRegistVO mapRegistByMapId = ampMapRegistMapper.findMapRegistByMapId(ampMapRegist);
        //redisUtils.del("gateway:mapurl:"+mapRegistByMapId.getMapCode());
        ampMapRegist = new AmpMapRegist();
        BeanUtils.copyProperties(ampMapRegistDTO,ampMapRegist);
        ampMapRegist.setVersion(new Date());
        ampMapRegist.setStatusCd(2);
        return ampMapRegistMapper.delete(ampMapRegist);
    }

    @Override
    public AmpMapRegistVO findMapRegistByMapId(AmpMapRegistDTO ampMapRegistDTO){
        AmpMapRegist ampMapRegist = new AmpMapRegist();
        BeanUtils.copyProperties(ampMapRegistDTO,ampMapRegist);
        return ampMapRegistMapper.findMapRegistByMapId(ampMapRegist);
    }

}
